var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgDomain: app.data.imgDomain,
    active: 1,
    items: [],
    page: 1,
    size: 20
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getItems()
  },
  /**
   * @purpose 获取预约列表
   */
  getItems () {
    wx.showLoading()
    app.request('get', 'wholeAppointmentItems', {
      page: this.data.page,
      size: this.data.size
    }, res => {
      this.setData({items: res})
    })
  },
  // scan: function () {
  //   wx.scanCode({
  //     success: (res) => {
  //       console.log(res)
  //     }
  //   })
  // },
  showActionSheet: function (event) {
    let item = event.currentTarget.dataset.item
    if (item.overdue === 1) {
      wx.showModal({
        title: '温馨提示',
        content: '未过期预约不可删除哦',
        showCancel: false
      })
      return false
    }
    wx.showActionSheet({
      itemList: ['删除'],
      success: res => {
        wx.showLoading()
        app.request('post', 'wholeAppointmentDelete', {
          id: item.id
        }, res => {
          wx.showToast({
            title: '删除成功',
            icon: 'none'
          })
          let index = Number(event.currentTarget.dataset.index)
          let items = this.data.items.filter((item, idx) => {
            if (idx === index) {
              return false;
            }
            return true;
          })
          this.setData({
            items: items
          })
        })
      },
      fail: function (res) {
        console.log(res.errMsg)
      }
    })
  },
  changeTab: function (event) {
    this.setData({
      active: Number(event.currentTarget.dataset.active)
    })
  }
})